<template>
  <section class="todoapp">
    <!-- 除了驼峰, 还可以使用-转换链接 -->
    <TodoHeader @on-add="btnAdd"></TodoHeader>
    <TodoMain :arr="list" @btn-del="btnDel"></TodoMain>
    <TodoFooter></TodoFooter>
  </section>
</template>

<script>
// 1.0 样式引入
import './styles/base.css'
import './styles/index.css'
import axios from 'axios'

import TodoHeader from './components/TodoHeader'
import TodoMain from './components/TodoMain'
import TodoFooter from './components/TodoFooter'
axios.defaults.baseURL = 'http://liufusong.top:3001'
export default {
  data () {
    return {
      list: []
    }
  },
  created () {
    this.getData()
    this.btnAdd()
  },

  components: {
    TodoHeader,
    TodoMain,
    TodoFooter
  },
  methods: {
    async getData () {
      try {
        const res = await axios({
          url: 'list'
        })
        this.list = res.data
        console.log(res)
      } catch (err) {
        alert('请求失败了')
      }
    },
    async btnAdd (e) {
      try {
        console.log(e)
        const res = await axios({
          url: 'add',
          method: 'POST',
          // data: {
          //   bookname: this.form.name,
          //   author: this.form.auth,
          //   publisher: this.form.publish
          // }
          data: {
            title: e
          }
        })
        console.log(res)
      } catch (err) {
        alert('请求失败了')
      }
    },
    async btnDel (e) {
      // console.log(e)
      // if (this.list.indexOf(e) === -1) {
      //   this.list.splice(this.list.indexOf(0), 1)
      // }
      try {
        const res = await axios({
          url: 'list/' + e,
          method: 'DELETE'
        })
        this.getData()
        console.log(res)
      } catch (err) {
        alert('请求失败了')
      }
    }
  }
}
</script>
